<template>
  <el-container class="add">
    <el-row>
      <el-col
        :span="24"
        style="display: flex; align-items: center; justify-content: center"
      >
        <p>在此页面，您可以{{ s }}优惠券</p>
        <el-button
          type="primary"
          style="position: absolute; right: 10%"
          @click="back"
          >返回<el-icon color="write" class="no-inherit"><ArrowRight /></el-icon
        ></el-button>
      </el-col>
    </el-row>
    <el-main class="main-1">
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
        :size="formSize"
        status-icon
      >
        <el-form-item label="优惠券名称" prop="name">
          <el-input v-model="ruleForm.name" style="width: 250px" />
        </el-form-item>
        <el-form-item label="类型" prop="state">
          <el-radio-group v-model="ruleForm.state">
            <el-radio label="课程表" />
            <el-radio label="商城表" />
          </el-radio-group>
        </el-form-item>
        <el-form-item label="面额" prop="money">
          <el-input v-model="ruleForm.money" style="width: 250px" />
        </el-form-item>
        <el-form-item label="发放数量" prop="num1">
          <el-input v-model="ruleForm.num1" style="width: 250px" />
        </el-form-item>
        <el-form-item label="每人可领数量" prop="num2">
          <el-input v-model="ruleForm.num2" style="width: 250px" />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :plain="true"
            @click="submitForm(ruleFormRef)"
            >保存</el-button
          >
          <el-button @click="resetForm(ruleFormRef)">取消</el-button>
        </el-form-item>
      </el-form>
    </el-main>
  </el-container>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import type { FormInstance, FormRules } from "element-plus";
import { ElMessage } from "element-plus";
import { ArrowRight } from "@element-plus/icons-vue";
import useStore from "../../store/ljh/index";
import { checkName, checkMoney, checkNum } from "./function/regexRules";

const route = useRoute();
const store = useStore();
const router = useRouter();
const Data = store.$state.ListVenues;
const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  name: "",
  state: "",
  money: "",
  num1: "",
  num2: "",
});
const s = route.query.msg;
const id = route.query.id;

onMounted(() => {
  console.log(id);
  console.log(Data);
});
// 返回上一级
const back = () => {
  router.push({ path: "CouponsView" });
};

const rules = reactive<FormRules>({
  name: [
    { required: true, message: "请输入优惠券名称", trigger: "blur" },
    { trigger: "blur", validator: checkName },
  ],
  money: [
    { required: true, message: "请输入优惠券金额", trigger: "blur" },
    { trigger: "blur", validator: checkMoney },
  ],
  num1: [
    { message: "请输入发放数量", trigger: "blur" },
    { trigger: "blur", validator: checkNum },
  ],
  num2: [
    { message: "请输入每人可领取数量", trigger: "blur" },
    { trigger: "blur", validator: checkNum },
  ],
  state: [
    {
      required: true,
      message: "请选择优惠券类型",
      trigger: "change",
    },
  ],
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      ElMessage({
        message: "保存成功",
        type: "success",
        duration: 1500,
      });
      setTimeout(() => {
        console.log("submit!");
        router.push("CouponsView");
      }, 2000);
    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  router.push("CouponsView");
};
</script>

<style scoped>
.add {
  margin-left: 10px;
  background-color: rgba(242, 242, 242, 1);
  display: flex;
  flex-direction: column;
  border-radius: 20px;
}
.main-1 {
  background-color: white;
  margin: 0 50px 50px 50px;
  border-radius: 20px;
}
</style>
